package smartcommunityserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import smartcommunityserver.bean.*;
import smartcommunityserver.service.BaseCountService;
import smartcommunityserver.service.TrafficChannelStatsService;
import smartcommunityserver.service.entranceTypeCountService;
import smartcommunityserver.service.minuteoutcountService;
import smartcommunityserver.utils.DateUtils;

import java.util.List;

@RestController
@RequestMapping("/sm/community")
public class CarCountController {

    // 常量定义：默认日期值
    private static final String DATE_DEFAULT_VALUE = "default";

    @Autowired
    public TrafficChannelStatsService trafficChannelStatsService;

    @RequestMapping("/carCount")
    public JSONObject getCarCount() {


        // 2. 调用Service层获取车辆统计数据
        List<CarCountBean> carCountList = trafficChannelStatsService.getCarCount();

        // 3. 构建JSON结构（三层嵌套：status, msg, data -> series -> data）
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);

        JSONArray seriesArray = new JSONArray();
        dataJson.put("series", seriesArray);

        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "series1");
        seriesArray.add(seriesObj);

        JSONArray dataArray = new JSONArray();
        seriesObj.put("data", dataArray);

        // 4. 填充数据
        for (CarCountBean carCount : carCountList) {
            JSONObject dataItem = new JSONObject();
            dataItem.put("name", carCount.getName());
            dataItem.put("value", carCount.getRatio());


            dataArray.add(dataItem);
        }

        return resultJson;
    }

    @Autowired
    smartcommunityserver.service.minuteoutcountService minuteoutcountService;

    @RequestMapping("/getMinuteOutCount")
    public JSONObject getMinuteOutCount() {

        List<minuteoutcountBean> minuteOutCount = minuteoutcountService.getMinuteOutCount();


        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");
        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);
        JSONArray categoriesArray = new JSONArray();
        JSONArray seriesArray = new JSONArray();
        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "统计日期");
        JSONArray dataArray = new JSONArray();
        for (minuteoutcountBean minuteoutcountBean : minuteOutCount) {
            categoriesArray.add(minuteoutcountBean.getMinute_time());

            dataArray.add(minuteoutcountBean.getOut_count());
        }
        seriesObj.put("data", dataArray);
        seriesArray.add(seriesObj);

        dataJson.put("categories", categoriesArray);
        dataJson.put("series", seriesArray);

        return resultJson;
    }

    //进入类型
    @Autowired
    smartcommunityserver.service.entranceTypeCountService entranceTypeCountService;

    @RequestMapping("/getEntranceTypeCount")
    public JSONObject getEntranceTypeCount() {
        List<entranceTypeCountBean> entranceTypeCount = entranceTypeCountService.getEntranceTypeCount();

        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");
        JSONArray dataArray = new JSONArray();
        resultJson.put("data", dataArray);
        for (entranceTypeCountBean entranceTypeCountBean : entranceTypeCount) {
            JSONObject item = new JSONObject();
            // 设置名称字段（对应分钟时间）
            item.put("name", entranceTypeCountBean.getName());
            // 设置数值字段（对应出车数量）
            item.put("value", entranceTypeCountBean.getEntrance_count());
            dataArray.add(item);
        }

        return resultJson;
    }

    @Autowired
    smartcommunityserver.service.OwnerWorkPlaceService ownerWorkPlaceService;

    @RequestMapping("/getOwnerWorkerPlace")
    public JSONObject getOwnerWorkerPlace() {
        List<OwnerWorkerPlaceBean> ownerWorkerPlace = ownerWorkPlaceService.getOwnerWorkerPlace();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");
        JSONObject jsonObject1 = new JSONObject();
        jsonObject.put("data",jsonObject1);

        JSONArray arr1 = new JSONArray();
        for (OwnerWorkerPlaceBean ownerWorkerPlaceBean : ownerWorkerPlace) {
            JSONObject jsonObject2 = new JSONObject();

            jsonObject2.put("name",ownerWorkerPlaceBean.getOwerWorkplace());
            jsonObject2.put("value",ownerWorkerPlaceBean.getOwerWorkcount());
            arr1.add(jsonObject2);
        }
        jsonObject1.put("mapData",arr1);
        return jsonObject;


    }


    @Autowired
    smartcommunityserver.service.minuteopenService minuteopenService;
    @RequestMapping("/getminuteopenCount")
    public JSONObject getminuteopenCount() {
        List<minuteopenBean> minuteopenBeans = minuteopenService.getminuteopenCount();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");
        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);
        JSONArray categoriesArray = new JSONArray();
        JSONArray seriesArray = new JSONArray();
        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "统计日期");
        JSONArray dataArray = new JSONArray();
        for (minuteopenBean minuteopenBean : minuteopenBeans) {
            categoriesArray.add(minuteopenBean.getMinute_time());

            dataArray.add(minuteopenBean.getOpen_m());
        }
        seriesObj.put("data", dataArray);
        seriesArray.add(seriesObj);

        dataJson.put("categories", categoriesArray);
        dataJson.put("series", seriesArray);

        return resultJson;
    }


    @Autowired
    BaseCountService baseCountService;
    @RequestMapping("/getbasiccount")
    public JSONObject getbasiccount() {
        List<BaseCountBean> getbasiccount = baseCountService.getbasiccount();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");
        JSONArray arr1 = new JSONArray();
        jsonObject.put("data",arr1);
        for (BaseCountBean baseCountBean : getbasiccount) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name",baseCountBean.getName());
            jsonObject1.put("value",baseCountBean.getCount());
            arr1.add(jsonObject1);
        }
        return jsonObject;


    }






}
